package com.example.administrator.demob;

import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    private HighPriorityReceiver highPriorityReceiver;
    private MiddlePriorityReceiver middlePriorityReceiver;
    private LowPriorityReceiver lowPriorityReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lowPriorityReceiver = new LowPriorityReceiver();
        IntentFilter lowFilter = new IntentFilter();
        lowFilter.addAction("LOCAL_BROADCAST");
        lowFilter.setPriority(0);
        registerReceiver(lowPriorityReceiver,lowFilter);

        middlePriorityReceiver = new MiddlePriorityReceiver();
        IntentFilter middleFilter = new IntentFilter();
        middleFilter.addAction("LOCAL_BROADCAST");
        middleFilter.setPriority(50);
        registerReceiver(middlePriorityReceiver,middleFilter);

        highPriorityReceiver = new HighPriorityReceiver();
        IntentFilter highFilter = new IntentFilter();
        highFilter.addAction("LOCAL_BROADCAST");
        highFilter.setPriority(100);
        registerReceiver(highPriorityReceiver,highFilter);

        //有序广播
        findViewById(R.id.btn_ordered).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction("ORDERED_BROADCAST");
                intent.putExtra("NAME","娜娜");
                intent.putExtra("AGE",18);
                sendOrderedBroadcast(intent,null);
            }
        });

        findViewById(R.id.btn_start).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction("WEATHER_INFO");
                sendBroadcast(intent);
            }
        });
    }
}
